package com.inforich.web.controller.receivereport;

import cn.hutool.core.collection.CollUtil;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.enums.BusinessType;
import com.inforich.common.utils.poi.ExcelUtil;
import com.inforich.receivereport.domain.RubbishReceiveReport;
import com.inforich.receivereport.domain.RubbishReceiveReportCell;
import com.inforich.receivereport.service.IRubbishReceiveReportService;

/**
 * 垃圾收纳量统计Controller
 *
 * @author liujie
 * @date 2022-11-22
 */
@RestController
@RequestMapping("/report/receivereport")
public class RubbishReceiveReportController extends BaseController {

  @Autowired
  private IRubbishReceiveReportService rubbishReceiveReportService;

  /**
   * 查询垃圾收纳量统计列表
   */
  @PreAuthorize("@ss.hasPermi('report:receivereport:list')")
  @GetMapping("/list")
  public Page<RubbishReceiveReportCell> list(Page<RubbishReceiveReportCell> page,
      RubbishReceiveReport rubbishReceiveReport) {
    Map<String, Object> map = beanToMap(rubbishReceiveReport);
    rubbishReceiveReportService.pageRubbishReceiveReportCell(page, map);
    RubbishReceiveReportCell totalCell = createTotalCell(page.getRecords());
    if (CollUtil.isNotEmpty(page.getRecords())) {
      page.getRecords().add(totalCell);
    }
    return page;
  }

  /**
   * 导出垃圾收纳量统计列表
   */
  @PreAuthorize("@ss.hasPermi('report:receivereport:export')")
  @Log(title = "垃圾收纳量统计", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, RubbishReceiveReport rubbishReceiveReport) {
    Map<String, Object> map = beanToMap(rubbishReceiveReport);
    List<RubbishReceiveReportCell> list = rubbishReceiveReportService.listRubbishReceiveReportCell(
        map);
    RubbishReceiveReportCell totalCell = createTotalCell(list);
    list.add(totalCell);
    ExcelUtil<RubbishReceiveReportCell> util = new ExcelUtil<RubbishReceiveReportCell>(
        RubbishReceiveReportCell.class);
    util.exportExcel(response, list, "垃圾收纳量统计");
  }

  /**
   * 合计
   *
   * @param cellList List<RubbishReceiveReportCell>
   * @return RubbishReceiveReportCell
   */
  private RubbishReceiveReportCell createTotalCell(List<RubbishReceiveReportCell> cellList) {
    RubbishReceiveReportCell totalCell = new RubbishReceiveReportCell();
    if (cellList == null || cellList.size() == 0) {
      return totalCell;
    }
    BigDecimal garbageWeightTotal = new BigDecimal("0");
    for (RubbishReceiveReportCell cell : cellList) {
      garbageWeightTotal = garbageWeightTotal.add(new BigDecimal(
          Optional.ofNullable(cell.getGarbageWeight()).orElse("0")));
    }

    totalCell.setDisposalLandName("合计:");
    totalCell.setGarbageWeight(garbageWeightTotal.toPlainString());

    return totalCell;
  }
}
